package com.wzg.appframe.net;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.wzg.appframe.utils.TLog;

import java.util.HashMap;
import java.util.Map;

public class VolleyRequest extends StringRequest {

    public String reqBody;
    Context mContext;

    public VolleyRequest(int method, String url, Response.Listener<String> listener,
                         Response.ErrorListener errorListener, Context context) {
        super(method, url, listener, errorListener);
        this.mContext = context;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Charset", "UTF-8");
        headers.put("Content-Type", "application/json");
        headers.put("Accept-Encoding", "gzip,deflate");
        return headers;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        return this.reqBody == null ? super.getBody() : this.reqBody.getBytes();
    }

    public void start() {
        setRetryPolicy(new DefaultRetryPolicy(30 * 1000, 0, 1.0f));
        TLog.d("net start url=" + getUrl());
        TLog.d("net start requestParma=" + this.reqBody);
        RequestQueue mQueue = Volley.newRequestQueue(this.mContext);
        mQueue.add(this);
//        mQueue.start();
    }

}
